package com.study.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.support.SessionStatus;

@Controller
@RequestMapping("/anno")
@SessionAttributes(value = "msg")//表示把msg的信息存入session域中，如果不加则只存到request的域中
public class AnnoController {

    @RequestMapping("/testRequestParam")
    public String testRequestParam(@RequestParam(value = "name",required = false) String username){
        System.out.println(username);
        return "success";
    }

    @RequestMapping("/testPathVariable/{id}")
    public String testPathVariable(@PathVariable(name = "id") String id,@CookieValue("JSESSIONID") String cookieId){
        System.out.println(id);
        System.out.println(cookieId);
        return "success";
    }
    @RequestMapping("/testModelAttribute")
    public String testModelAttribute(){
        System.out.println("testModelAttribute...");
        return "success";
    }

    @ModelAttribute//加了这个注释的这个方法优先于其它方法先执行
    public void showUser(){
        System.out.println("showUser...");
    }


    @RequestMapping("/testSessionAttributes")
    public String testSessionAttributes(Model model){
        System.out.println("testSessionAttributes...");

        //会被存在session中
        model.addAttribute("msg","aaaaaaaaaa");
        return "success";
    }

    @RequestMapping("/getSessionAttributes")
    public String getSessionAttributes(ModelMap modelMap){
        System.out.println("getSessionAttributes...");

        String msg = (String) modelMap.get("msg");
        System.out.println(msg);
        return "success";
    }

    @RequestMapping("/delSessionAttributes")
    public String delSessionAttributes(SessionStatus status){
        System.out.println("delSessionAttributes...");
        status.setComplete();//把session清除
        return "success";
    }

}
